﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace TetrisClient
{
    /// <summary>
    /// This class is responsible to execute method that manipulates the GUI, 
    /// since new threads can not modify the GUI directly.
    /// </summary>
    public class ThreadActionExecutor
    {
        private static readonly Dispatcher dispatcher;

        /// <summary>
        /// Initializes the <see cref="ThreadActionExecutor"/> class.
        /// </summary>
        static ThreadActionExecutor()
        {
            dispatcher = Deployment.Current.Dispatcher;
        }

        /// <summary>
        /// This method receives a reference to a method to be executed.
        /// </summary>
        /// <param name="action">The action.</param>
        public static void execute(Action action)
        {
            if (dispatcher.CheckAccess())
            {
                action.Invoke();
            }
            else
            {
                dispatcher.BeginInvoke(action);
            }
        }
    }
}
